package com.lianniu.mgr.product.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductModelVo;
import com.lianniu.mgr.product.service.IProductModelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/rest/ProductModel")
public class ProductModelRestController {
	
	private final static Logger logger = Logger.getLogger(ProductModelRestController.class);
	  
	
	  @Autowired
	  private IProductModelService ProductModelService;
	  
	  @RequestMapping(value="/ProductModelList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult ProductModelList(HttpServletRequest request, ProductModelVo curVo, PaginationInfo paginationInfo)
	  {
	       logger.info("ProductModelRestController  ProductModelList begin.");
	       try {
	           
	           logger.info("ProductModelList begin.");          
	           BaseResult baseResult = ProductModelService.selectByProductModelForPage(curVo, paginationInfo);
	           logger.info("ProductModelList end.");
	           logger.info("ProductModelRestController ProductModelList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ProductModelRestController ProductModelList error:" + e.getMessage());
	       logger.error("ProductModelRestController ProductModelList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	  
	  @RequestMapping(value = {"/ProductModelAdd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult dictionaryAdd(HttpServletRequest request,ProductModelVo curVo) {
	      logger.info("ProductModelRestController ProductModelAdd begin.");      
	      logger.info("ProductModelRestController ProductModelAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ProductModelAdd begin.");
	          
	          BaseResult baseResult = ProductModelService.insertProductModel(curVo);
	          logger.info("ProductModelAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductModelAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductModelAdd error:"+e.getMessage());
	      logger.info("ProductModelRestController ProductModelAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  } 
	  
	  
	  @RequestMapping(value = {"/ProductModelEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductModelEdit(HttpServletRequest request,ProductModelVo curVo) {
	      logger.info("ProductModelRestController ProductModelEdit begin.");      
	      logger.info("ProductModelRestController ProductModelEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ProductModelEdit begin.");
	          BaseResult baseResult = ProductModelService.updateById(curVo);
	          logger.info("ProductModelEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductModelEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductModelEdit error:"+e.getMessage());
	      logger.info("ProductModelRestController ProductModelEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }  
	  
	  
	  @RequestMapping(value="/selectProductModelById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectProductModelById(HttpServletRequest request,ProductModelVo curVo) {
	        logger.info("ProductModelRestController selectProductModelById begin.");
	        try {
	                    
	          BaseResult baseResult = ProductModelService.selectById(curVo);
	          logger.info("selectDictionaryById end.");
	          logger.info("ProductModelRestController selectProductModelById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductModelRestController selectProductModelById error:" + e.getMessage());
	          logger.error("ProductModelRestController selectProductModelById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value = {"/ProductModelDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductModelDel(HttpServletRequest request) {
	      logger.info("ProductModelRestController ProductModelDel begin.");
	     try {          
	          logger.info("ProductModelDel begin.");
	          BaseResult baseResult = ProductModelService.deleteProductModelBatch(request.getParameter("ids").split(","));	        
	          logger.info("ProductModelDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductModelDel end.");
	          
	          logger.info("ProductModelRestController ProductModelDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductModelRestController ProductModelDel error:" + e.getMessage());
	      logger.info("ProductModelRestController ProductModelDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  
	  
	  @RequestMapping(value = {"/checkCateName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkCateName(HttpServletRequest request) {
	      Boolean flag=true;
	      String cateName=  request.getParameter("cateName");
	      logger.info("ProductModelRestController checkCateName begin.");
	     try {          
	          logger.info("checkCateName begin.");
	          if("".equalsIgnoreCase(cateName)||cateName==null)
	          {return flag;}
	          flag = ProductModelService.checkCateName(cateName);         
	          logger.info("checkCateName end.");
	          
	          logger.info("ProductModelRestController checkCateName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductModelRestController checkCateName error:" + e.getMessage());
	      logger.info("ProductModelRestController checkCateName end.");
	      return flag;
	    }

	  }
	  
	  
	  @RequestMapping(value = {"/checkCateNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkCateNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String cateName=  request.getParameter("cateName");
	      String cateId=request.getParameter("cateId");
	      logger.info("ProductModelRestController checkCateNameEdit begin.");
	     try {          
	          logger.info("checkTypeNameEdit begin.");
	          ProductModelVo vo=new ProductModelVo();	         
	          vo.setCateId(cateId);
	          
	          BaseResult baseResult =ProductModelService.selectById(vo);	          
	          ProductModelVo curVo=(ProductModelVo) baseResult.getData();
	          if(curVo.getCateId().equals(cateId)&&curVo.getCateName().equals(cateName))
	          {return flag;}
	          if("".equalsIgnoreCase(cateName)||cateName==null)
	          {return flag;}
	          flag = ProductModelService.checkCateName(cateName);         
	          logger.info("checkCateNameEdit end.");
	          
	          logger.info("ProductModelRestController checkCateNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductModelRestController checkCateNameEdit error:" + e.getMessage());
	      logger.info("ProductModelRestController checkCateNameEdit end.");
	      return flag;
	    }

	  }
	  @RequestMapping(value="/selectParentBrand",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectParentBrand(HttpServletRequest request) {
	        logger.info("ProductModelRestController selectParentCate begin.");
	        try {
	        	
	          String cateId = request.getParameter("cateId");          
	          BaseResult baseResult = ProductModelService.selectParentBrand(cateId);
	          logger.info("selectParentCate end.");
	          logger.info("ProductCateRestController selectParentCate end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectParentCate error:" + e.getMessage());
	          logger.error("ProductCateRestController selectParentCate end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 

	  @RequestMapping(value="/selectProductPicByCateId",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectProductPicByCateId(HttpServletRequest request) {
	        logger.info("ProductModelRestController selectProductPicByCateId begin.");
	        try {
	        	
	          String cateId = request.getParameter("cateId");          
	          BaseResult baseResult = ProductModelService.selectProductPicByCateId(cateId);
	          logger.info("selectProductPicByCateId end.");
	          logger.info("ProductCateRestController selectProductPicByCateId end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectProductPicByCateId error:" + e.getMessage());
	          logger.error("ProductCateRestController selectProductPicByCateId end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
}
